import CustOrderService from "@/api/order/CustOrderService";
import common from "@/utils/common";
const PAGESIZE = 12;
const orders = {
	namespaced: true,
	state: {
		//控制功能树子菜单
		treeValue: "DEAL",
		unTreeList: [],
		// 分页
		pageInfo: {
			pageIndex: 1,
			pageSize: PAGESIZE,
		},
		// //可欣版订单类型：DEAL:待我处理，MY:我的处理，MYDEAL:我已处理，JTALL:所有，50030011 我的预警,我的关注:DGZ
		// orderStage: "",
		//订单类型：DEAL:待我处理，MY:我的处理，MYDEAL:我已处理，JTALL:所有，WARN 我的预警,我的关注:DGZ
		// orderType: "",
		dealType: "DEAL", //新新版:功能树第一级目录编码:DEAL:待我处理，MY:我的处理，MYDEAL:我已处理，JTALL:所有，WARN 我的预警,我的关注:DGZ
		orderStage: "",
		orderStageId: "", //新新版:
		//单选按钮切换
		selectType: "",
		//切换筛选类型更改这个值
		searchType: "1",
		//查询的文本框中 ：任何类型下都存入该字段
		searchText: "",
		//单选按钮切换时：1,所有，２，到预警期，３到期，４超期
		stage: "1",
		showTypeName: "un-order-list",
		listLoading:false,// 工单级别加载控制

		//高级查询部分
		B_REQ_AUD_DATE: "", // 需求单审核日期开始
		E_REQ_AUD_DATE: "", // 需求单审核日期结束

		B_ORDER_AUD_DATE: "", // 订单审核日期开始
		E_ORDER_AUD_DATE: "", // 订单审核日期结束

		B_FINISH_TIME: "", // 竣工日期开始
		E_FINISH_TIME: "", // 竣工日期结束

		B_FINISH_OPER_DATE: "", // 填报竣工日期开始
		E_FINISH_OPER_DATE: "", // 填报竣工日期结束

		B_BEGIN_RENT_TIME: "", // 起租日期开始
		E_BEGIN_RENT_TIME: "", // 起租日期结束

		B_HANDLE_TIME: "", // 受理日期开始
		E_HANDLE_TIME: "", // 受理日期结束

		CUST_SO_NUMBER: "", // 流水号
		CUST_SO_NUMBER_TYPE: "1", // 1精确查询 2模糊查询

		CUST_NUMBER: "", // 客户编码
		CUST_NUMBER_TYPE: "1", // 1精确查询 2模糊查询

		ACC_NBR: "", // 电路代号/设备号
		ACC_NBR_TYPE: "1", // 1精确查询 2模糊查询

		CUST_NAME: "", // 客户名称
		CUST_NAME_TYPE: "1", // 1精确查询 2模糊查询

		// --一下字段在页面还未找到搜集的地方---
		//
		qryType: "1",
		//
		qryValue: "",

		orderCol: "",
		//
		sortTyper: "",
		//
		paramData: "",
		//
		flag: "",
		//
		saleId: "",
		//
		locationId: "",
		//工单信息列表
		datasource: { customerOrder: null, pageInfo: { pageIndex: null, rowCount: null } },
		//工单信息销售品信息列表
		orderItemList: null,
        //工单信息销售品信息总数
        orderRowCount:{
			pageIndex: 1,
			pageSize: 10,
			pageCount: 0,
            rowCount: 0
		},

		// 工单信息订单列表单选
		custOrderRaidoA: '',
		// 客户定位弹窗点击列表对应数据存储
		orderPosInfo: {
			// 临时，暂时存储
			statusCds: "200000,300000",
			custMsg: null,
			custId: null,
			custName: "",
		},
		// 客户定位订购标识参数存储
		elecLabel: null,
		// 省客户id 存储
		respCustId: null,
		// 订购顶部步骤导航状态存储
		stepNav: {
			active: 1,
			stepData: [
				{
					title: "客户定位",
					router: "/custinfo",
				},
				{
					title: "新建订单",
					router: "/orderadd",
				},
				{
					title: "信息录入",
					router: "/inforentryppm",
				},
				{
					title: "订单保存",
					router: "",
				},
			]
		},
		// 订购改单系列状态存储
		stepNav2: {
			active: 1,
			stepData: [
				{
					title: "客户信息",
					router: "/custinfo",
				},
				{
					title: "已改单销售品列表",
					router: "/bgorderadd",
				},
				{
					title: "继续改单",
					router: "/continuechange",
				}
			]
		},
		//非变更项列表选中
		CARRY_ROW: {},
		changeConfig:{},
		CUST_INFO_CHANGE:{},
	},

	mutations: {
		// 工单级别销售品列表加载条改变
		SET_LIST_LOADING:(state, value) => {
			state.listLoading = value;
		},
		// 客户定位订购标识参数改变
		SET_ELE_LABEL:(state, value) => {
			state.elecLabel = value;
		},
		// 省客户id改变
		SET_RESP_CUSTID:(state, value) => {
			state.respCustId = value;
		},
		// 工单信息订单列表单选改变
		SET_CUST_ORDER_RADIOA: (state, value) => {
			state.custOrderRaidoA = value;
		},
		// 订购顶部改单系列步骤导航active改变
		SET_STEPACTIVE2: (state, value) => {
			state.stepNav2.active = value
		},
		// 订购顶部步骤导航active改变
		SET_STEPACTIVE: (state, value) => {
			state.stepNav.active = value
		},
		// 订购顶部步骤导航prodId改变
		// SET_STEPPRODID: (state, value) => {
		// 	state.stepNav.stepData[2].router = "/inforentryppm/" + value
		// },
		//功能树列表
		SET_UNTREELIST: (state, value) => {
			state.unTreeList = value;
		},
		// 客户定位弹窗列表点击改变
		SET_CUSTID: (state, value) => {
			state.orderPosInfo.custId = value;
		},
		// 客户定位弹窗列表点击改变
		SET_CUSTNAME: (state, value) => {
			state.orderPosInfo.custName = value;
		},
		// 客户定位信息存储
		SET_CUSTMSG: (state, value) => {
			state.orderPosInfo.custMsg = value;
		},
		// 分页改变pageSize和pageIndex
		SET_PAGEINFO: (state, value) => {
			state.pageInfo = value;
		},
		//分页改变pageSize
		SET_PAGESIZE: (state, value) => {
			state.pageInfo.pageSize = value;
		},
		// 分页改变pageIndex
		SET_PAGEINDEX: (state, value) => {
			state.pageInfo.pageIndex = value;
		},
		SET_ORDERTYPE: (state, value) => {
			state.orderType = value;
		},
		SET_DEALTYPE: (state, dealType) => {
			state.dealType = dealType;
		},
		SET_DATASOURCE: (state, datasource) => {
			state.datasource = datasource;
		},
		SET_ORDERITEMLIST: (state, orderItemList) => {
			state.orderItemList = orderItemList;
		},
        SET_ORDERITEMROWCOUNT: (state, orderRowCount) => {
            state.orderRowCount = orderRowCount;
        },

		SET_SELECTTYPE: (state, value) => {
			state.selectType = value;
		},
		SET_SEARCHTEXT: (state, value) => {
			state.searchText = value;
		},
		SET_STAGE: (state, value) => {
			state.stage = value;
		},
		SET_SHOW_TYPE_NAME: (state, value) => {
			state.showTypeName = value;
		},
		SET_SEARCHTYPE: (state, value) => {
			state.searchType = value;
		},
		SET_QRYTYPE: (state, value) => {
			state.qryType = value;
		},
		SET_QRYVALUE: (state, value) => {
			state.qryValue = value;
		},
		SET_ORDERSTAGE: (state, value) => {
			state.orderStage = value;
		},
		SET_ORDERSTAGEID: (state, value) => {
			state.orderStageId = value;
		},
		SET_ORDERCOL: (state, value) => {
			state.orderCol = value;
		},
		SET_SORTTYPER: (state, value) => {
			state.sortTyper = value;
		},
		SET_PARAMDATA: (state, value) => {
			state.paramData = value;
		},
		SET_FLAG: (state, value) => {
			state.flag = value;
		},
		SET_SALEID: (state, value) => {
			state.saleId = value;
		},
		SET_LOCATIONID: (state, value) => {
			state.locationId = value;
		},

		//高级查询部分
		SET_B_REQ_AUD_DATE: (state, value) => {
			state.B_REQ_AUD_DATE = value;
		},
		SET_E_REQ_AUD_DATE: (state, value) => {
			state.E_REQ_AUD_DATE = value;
		},
		SET_B_ORDER_AUD_DATE: (state, value) => {
			state.B_ORDER_AUD_DATE = value;
		},
		SET_E_ORDER_AUD_DATE: (state, value) => {
			state.E_ORDER_AUD_DATE = value;
		},
		SET_B_FINISH_TIME: (state, value) => {
			state.B_FINISH_TIME = value;
		},
		SET_E_FINISH_TIME: (state, value) => {
			state.E_FINISH_TIME = value;
		},
		SET_B_FINISH_OPER_DATE: (state, value) => {
			state.B_FINISH_OPER_DATE = value;
		},
		SET_E_FINISH_OPER_DATE: (state, value) => {
			state.E_FINISH_OPER_DATE = value;
		},
		SET_B_BEGIN_RENT_TIME: (state, value) => {
			state.B_BEGIN_RENT_TIME = value;
		},
		SET_E_BEGIN_RENT_TIME: (state, value) => {
			state.E_BEGIN_RENT_TIME = value;
		},
		SET_B_HANDLE_TIME: (state, value) => {
			state.B_HANDLE_TIME = value;
		},
		SET_E_HANDLE_TIME: (state, value) => {
			state.E_HANDLE_TIME = value;
		},
		SET_CUST_SO_NUMBER: (state, value) => {
			state.CUST_SO_NUMBER = value;
		},
		SET_CUST_SO_NUMBER_TYPE: (state, value) => {
			state.CUST_SO_NUMBER_TYPE = value;
		},
		SET_CUST_NUMBER: (state, value) => {
			state.CUST_NUMBER = value;
		},
		SET_CUST_NUMBER_TYPE: (state, value) => {
			state.CUST_NUMBER_TYPE = value;
		},
		SET_ACC_NBR: (state, value) => {
			state.ACC_NBR = value;
		},
		SET_ACC_NBR_TYPE: (state, value) => {
			state.ACC_NBR_TYPE = value;
		},
		SET_CUST_NAME: (state, value) => {
			state.CUST_NAME = value;
		},
		SET_CUST_NAME_TYPE: (state, value) => {
			state.CUST_NAME_TYPE = value;
		},
		SET_CARRY_ROW: (state, value) => {
			state.CARRY_ROW = value;
		},
		SET_CHANGE_CONFIG:(state, value) => {
			state.changeConfig = value;
		},
		SET_CUST_INFO_CHANGE:(state, value) => {
			state.CUST_INFO_CHANGE = value;
		},
		
	},

	actions: {
		/**
		 * 客户订单列表请求
		 * @param {} param
		 * @param {*} userInfo
		 * @returns
		 */
		async qryPendingOrderList({ commit }, conditions) {
			let newConditions = {};
			for(var key in conditions){
				newConditions[common.toHump(key)] = conditions[key];
			}
			commit("SET_LIST_LOADING", true);
			let result = await CustOrderService.qryPendingOrderList(newConditions);
			commit("SET_LIST_LOADING", false);
			if (result.code == 200) {
				commit("SET_DATASOURCE", result.meta);

				// commit("orders/SET_CUST_ORDER_RADIOA", '');
				this._modules.root.state.orders.custOrderRaidoA="";
				// this.SET_CUST_ORDER_RADIOA("orders","");
				localStorage.setItem("radionSelectedcustormer", "[]");

			} else {
				this.$message({
					showClose: true,
					message: 'code:' + result.code + '  msg:' + result.msg,
					type: 'error'
				});
			}


		},

		/**
		 * 工单信息销售品信息查询(废弃)
		 */
		async qryOrderItem({ commit }, conditions) {
			let result = await CustOrderService.qryOrderItem(conditions);
			if (result.code == 200) {
				commit("SET_ORDERITEMLIST", result.meta.resultObject.offerOrderItems);
			}
		},

		/**
		 * 工单信息销售品信息查询(新改)
		 * @param {*} param0
		 * @param {*} conditions
		 */
		async qryPendingOrderItemList({ commit }, conditions) {
			commit("SET_ORDERITEMLIST", []);

			let result = await CustOrderService.qryPendingOrderItemList(conditions);
			if (result.meta.resultCode == '0') {
				commit("SET_ORDERITEMLIST", result.meta.offerOrderItems);
                commit("SET_ORDERITEMROWCOUNT", result.meta.pageInfo);
			}
		},

		/**
		 * 功能树查询
		 * @param {*} param0
		 * @param {*} conditions
		 */
		async qryPendingOrderTree({ commit }, cods) {
			let{conditions,countConditions} = cods;

			// if (dealType.split(":")[0] != "DEAL") {
			// if (dealType != "DEAL") {
			// 	// 临时代码：只显示待我处理功能项
			// 	commit("SET_UNTREELIST", []);
			// 	return;
			// } else {
			// conditions = {
			// 	orderStage: "50030002",
			// 	systemUserId: "7777777",
			// };
			let result = await CustOrderService.qryPendingOrderTree(conditions);
			const list = result.meta.pendingOrderTree;
			for (let i = 0; i < list.length; i++) {
				list[i].orderCount = 0;
			}
			commit("SET_UNTREELIST", list);
			if(orders.state.dealType == "DEAL"){
				for (let i = 0; i < list.length; i++) {
						let stageId = list[i].orderStageId;
						let countParams = {
							taskNumReq: [{stageId,...countConditions}],
						};
						let res = await CustOrderService.qryPendingOrderCount(countParams);
						list[i].orderCount = res.meta.taskNumRes[0].orderCount;
				}
			}
			// }
		},

		/**
		 * 清空查询条件
		 * @param {} param0
		 */
		clearQryConditions({ commit }) {
			commit("SET_ORDERTYPE", "");
			commit("SET_SELECTTYPE", "");
			commit("SET_SEARCHTEXT", "");
			commit("SET_STAGE", "1");
			commit("SET_SEARCHTYPE", "");
			commit("SET_QRYTYPE", 1);
			// commit("SET_QRYVALUE", "");
			commit("SET_ORDERSTAGE", "");
			commit("SET_ORDERSTAGEID", "");
			commit("SET_ORDERCOL", "");
			commit("SET_SORTTYPER", "");
			commit("SET_PARAMDATA", "");
			commit("SET_FLAG", "");
			commit("SET_SALEID", "");
			commit("SET_LOCATIONID", "");
			commit("SET_B_REQ_AUD_DATE", "");
			commit("SET_E_REQ_AUD_DATE", "");
			commit("SET_B_ORDER_AUD_DATE", "");
			commit("SET_E_ORDER_AUD_DATE", "");
			commit("SET_B_FINISH_TIME", "");
			commit("SET_E_FINISH_TIME", "");
			commit("SET_B_FINISH_OPER_DATE", "");
			commit("SET_E_FINISH_OPER_DATE", "");
			commit("SET_B_BEGIN_RENT_TIME", "");
			commit("SET_E_BEGIN_RENT_TIME", "");
			commit("SET_B_HANDLE_TIME", "");
			commit("SET_E_HANDLE_TIME", "");
			commit("SET_CUST_SO_NUMBER", "");
			commit("SET_CUST_SO_NUMBER_TYPE", "1");
			commit("SET_CUST_NUMBER", "");
			commit("SET_CUST_NUMBER_TYPE", "1");
			commit("SET_ACC_NBR", "");
			commit("SET_ACC_NBR_TYPE", "1");
			commit("SET_CUST_NAME", "");
			commit("SET_CUST_NAME_TYPE", "1");
			commit("SET_PAGEINDEX", 1);
		}
	},
};

export default orders;
